﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Linq;

namespace JetRetail
{
    public partial class RedeemCertificateForm : Form
    {
        int iCheckKey;
        string sCheckNumber;
        decimal deTotalAmount, deCertificateAmount = 0;
        DrvFRLib.IDrvFR47 ShtrihM;
        JetRetailDB DB;
        int iContractor = 0;
        JetRetail.WorkSetting WorkSet;
        private List<decimal> lstSumCertificate = new List<decimal>();
        private List<int> lstContractor = new List<int>();
        private int iRegistratorType;
  

        string DecimalToString(decimal deNumber)
        {
            int i, j;
            if (deNumber >= 1000000000)
            {
                i = 10;
                j = 1000000000;
            }
            else if (deNumber >= 100000000)
            {
                i = 9;
                j = 100000000;
            }
            else if (deNumber >= 10000000)
            {
                i = 8;
                j = 10000000;
            }
            else if (deNumber >= 1000000)
            {
                i = 7;
                j = 1000000;
            }
            else if (deNumber >= 100000)
            {
                i = 6;
                j = 100000;
            }
            else if (deNumber >= 10000)
            {
                i = 5;
                j = 10000;
            }
            else if (deNumber >= 1000)
            {
                i = 4;
                j = 1000;
            }
            else if (deNumber >= 100)
            {
                i = 3;
                j = 100;
            }
            else if (deNumber >= 10)
            {
                i = 2;
                j = 10;
            }
            else
            {
                i = 1;
                j = 1;
            }

            string sNumber = "";
            for (int k = 0; k < i; k++)
            {
                sNumber += Convert.ToString(Convert.ToInt32(deNumber) / j);
                if ((k != i - 1) && ((i - k) % 3 == 1)) sNumber += " ";
                deNumber %= j;
                j /= 10;
            }

            return sNumber;
        }

        public RedeemCertificateForm(JetRetailDB DB, int iCheckKey, string sCheckNumber, decimal deTotalAmount, DrvFRLib.IDrvFR47 ShtrihM)
        {
            this.DB = DB;
            this.iCheckKey = iCheckKey;
            this.sCheckNumber = sCheckNumber;
            this.deTotalAmount = deTotalAmount;

            this.ShtrihM = ShtrihM;
            WorkSet = new WorkSetting();

            iRegistratorType = WorkSet.GetRegistratorType();

            InitializeComponent();
        }

        bool PrepareCheckPositionsRegistration()
        {
            try
            {
                switch (1)
                {
                    /*case 1:
                        {
                            Atol.Password = "1";
                            Atol.Mode = 1;
                            if (Atol.SetMode() != 0)
                            {
                                MessageBox.Show(Atol.ResultDescription, "Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return false;
                            }

                            Atol.GetStatus();
                            if ((Atol.CheckState != 0) && (Atol.CancelCheck() != 0))
                            {
                                MessageBox.Show(Atol.ResultDescription, "Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return false;
                            }
                        }
                        break;*/

                    case 1:
                    case 2:
                        {
                            //ShtrihM.Password = 30;
                            ShtrihM.Password = WorkSet.GetSchtrichPass(); 
                            ShtrihM.SysAdminCancelCheck();

                            /*ShtrihM.FirstLineNumber = 1;
                            ShtrihM.LastLineNumber = 200;
                            if (ShtrihM.Draw() != 0) MessageBox.Show(ShtrihM.ResultCodeDescription, "Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Error);*/

                            //ShtrihM.Password = 1;
                            ShtrihM.Password = WorkSet.GetSchtrichPassKassir(); 
                            ShtrihM.CheckType = 0;
                            if (ShtrihM.OpenCheck() != 0)
                            {
                                MessageBox.Show(ShtrihM.ResultCodeDescription, "Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return false;
                            }
                        }
                        break;
                }

                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }

        bool RegisterCheckPosition(string sName, decimal deSumWithoutDiscount, decimal deSumWithDiscount, double dQty)
        {
            try
            {
                switch (1)
                {
                    /*case 1:
                        {
                            Atol.Name = sName;
                            Atol.Quantity = 1;
                            Atol.Price = Convert.ToDouble(deSumWithoutDiscount);
                            Atol.DiscountType = 0;
                            Atol.DiscountValue = Convert.ToDouble(deSumWithoutDiscount - deSumWithDiscount);
                            if (Atol.Registration() != 0)
                            {
                                MessageBox.Show(Atol.ResultDescription, "Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return false;
                            }
                        }
                        break;*/

                    case 1:
                    case 2:
                        {
                            ShtrihM.StringForPrinting = sName;
                            if ((((int)((double)deSumWithoutDiscount / (dQty * 10))) * 10 * dQty) == (double)deSumWithoutDiscount)
                            {
                                ShtrihM.Quantity = dQty;
                                ShtrihM.Price = deSumWithoutDiscount / (decimal)dQty;
                            }
                            else
                            {
                                ShtrihM.Quantity = 1;
                                ShtrihM.Price = deSumWithoutDiscount;
                            }
                            if (ShtrihM.SaleEx() != 0)
                            {
                                MessageBox.Show(ShtrihM.ResultCodeDescription, "Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return false;
                            }

                            if (deSumWithDiscount != deSumWithoutDiscount)
                            {
                                ShtrihM.Summ1 = deSumWithoutDiscount - deSumWithDiscount;
                                ShtrihM.StringForPrinting = "";
                                if (ShtrihM.Discount() != 0)
                                {
                                    MessageBox.Show(ShtrihM.ResultCodeDescription, "Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    return false;
                                }
                            }
                        }
                        break;
                }

                return true;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }

        private bool RegisterCheckPositions()
        {
            decimal deTotalDiscount = 0;

            try
            {
                if (PrepareCheckPositionsRegistration() == false) return false;
                else
                {
                    //ISingleResult<ПолучитьПозицииЧекаResult> Result = DB.ПолучитьПозицииЧека(iCheckKey);
                    ISingleResult<ПолучитьПозицииЧекаResult> Result = DB.GetPositionCheck(iCheckKey);
                    foreach (ПолучитьПозицииЧекаResult DataSet in Result)
                    {
                        string sName = DataSet.Название;
                        //string sQty = "   * " + ((DataSet.ЦелоеКоличество == 0) ? ("") : ("" + DataSet.ЦелоеКоличество)) + ((DataSet.ДробноеКоличество % DataSet.ДелимостьНаЧасти == 0) ? ("") : (((DataSet.ЦелоеКоличество == 0) ? ("") : (" и ")) + DataSet.ДробноеКоличество + "/" + DataSet.ДелимостьНаЧасти));
                        if (sName.Length > 40 - /*sQty.Length*/10) sName = sName.Substring(0, 40 - /*sQty.Length*/10);
                        //sName += sQty;

                        ShtrihM.UseReceiptRibbon = true;
                        ShtrihM.UseJournalRibbon = true;
                        ShtrihM.StringForPrinting = "------------------------------------";
                        ShtrihM.PrintString();
                        ShtrihM.StringForPrinting = DataSet.Название.Substring(0, (DataSet.Название.Length <= 36) ? (DataSet.Название.Length) : (36));
                        ShtrihM.PrintString();
                        if (DataSet.Характеристики.Length > 0)
                        {
                            ShtrihM.StringForPrinting = DataSet.Характеристики;
                            ShtrihM.PrintString();
                        }

                        if (RegisterCheckPosition("", Convert.ToDecimal(DataSet.СуммаБезСкидки), Convert.ToDecimal(DataSet.СуммаСоСкидкой), (double)(DataSet.ЦелоеКоличество + ((double)DataSet.ДробноеКоличество) / ((double)DataSet.ДелимостьНаЧасти))) == false) return false;

                        deTotalDiscount += (DataSet.СуммаБезСкидки.Value - DataSet.СуммаСоСкидкой.Value);
                    }

                    ShtrihM.StringForPrinting = "------------------------------------";
                    ShtrihM.PrintString();

                    return true;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                decimal deCashSum = 0;
                decimal deSertificateSum = 0;

                if (iRegistratorType == 3 || iRegistratorType == 4)
                {
                    deCashSum = (deTotalAmount - deCertificateAmount <= 0) ? (0) : (deTotalAmount - deCertificateAmount);
                    deSertificateSum = (deCertificateAmount > deTotalAmount) ? (deTotalAmount) : (deCertificateAmount);

                    MainForm.Instance.OpenDrawer(); // открываем денежный ящик

                    if (MainForm.Instance.RegisterCheckPositions(deCashSum, 0, true, false, null, deSertificateSum, ""))
                    {
                        string sCheckNumber = MainForm.Instance.GetRegistratorCheckNumber();

                        //if (CloseRegistratorCheck(deAccepted, 0) == true) CloseCheck(sCheckNumber, 0);

                        // ориганальный вариант
                        //DB.ЗакрытьЧек(iCheckKey, sCheckNumber, deTotalAmount, deTotalAmount, 0);

                        try
                        {
                            //DB.ЗакрытьЧек(iCheckKey, sCheckNumber, deTotalAmount, (iPaymentType == 0) ? (deAccepted) : (0)); // ориганальный вариант 
                            DB.ЗакрытьЧек(iCheckKey, sCheckNumber, deTotalAmount, deTotalAmount, 0);
                        }
                        catch (Exception e2)
                        {
                            try
                            {
                                DB.ЗакрытьЧек(iCheckKey, sCheckNumber, deTotalAmount, deTotalAmount); // ориганальный (старый) вариант 
                            }
                            catch (Exception)
                            {
                            }
                        } 


                        DB.ПривязатьСертификат(iCheckKey, iContractor,(deCertificateAmount > deTotalAmount)
                                                   ? (deTotalAmount)
                                                   : (deCertificateAmount));
                        DialogResult = DialogResult.OK;
                        Close();
                    }
                    return;
                }

                //======================================
                decimal deTotalDiscount = 0;

                //ShtrihM.Password = 30;
                ShtrihM.Password = WorkSet.GetSchtrichPass();
                ShtrihM.SysAdminCancelCheck();

                //ShtrihM.Password = 1;
                ShtrihM.Password = WorkSet.GetSchtrichPassKassir();
                ShtrihM.CheckType = 0;
                if (ShtrihM.OpenCheck() != 0)
                    MessageBox.Show(ShtrihM.ResultCodeDescription, "Jet-Розница", MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                else
                {
                    //ISingleResult<ПолучитьПозицииЧекаResult> Result = DB.ПолучитьПозицииЧека(iCheckKey);
                    ISingleResult<ПолучитьПозицииЧекаResult> Result = DB.GetPositionCheck(iCheckKey);

                    foreach (ПолучитьПозицииЧекаResult DataSet in Result)
                    {
                        string sName = DataSet.Название;
                        //string sQty = "   * " + ((DataSet.ЦелоеКоличество == 0) ? ("") : ("" + DataSet.ЦелоеКоличество)) + ((DataSet.ДробноеКоличество % DataSet.ДелимостьНаЧасти == 0) ? ("") : (((DataSet.ЦелоеКоличество == 0) ? ("") : (" и ")) + DataSet.ДробноеКоличество + "/" + DataSet.ДелимостьНаЧасти));
                        if (sName.Length > 40 - /*sQty.Length*/ 10) sName = sName.Substring(0, 40 - /*sQty.Length*/ 10);
                        //sName += sQty;

                        ShtrihM.UseReceiptRibbon = true;
                        ShtrihM.UseJournalRibbon = true;
                        ShtrihM.StringForPrinting = "------------------------------------";
                        ShtrihM.PrintString();
                        ShtrihM.StringForPrinting = DataSet.Название.Substring(0,
                                                                               (DataSet.Название.Length <= 36)
                                                                                   ? (DataSet.Название.Length)
                                                                                   : (36));
                        ShtrihM.PrintString();
                        if (DataSet.Характеристики.Length > 0)
                        {
                            ShtrihM.StringForPrinting = DataSet.Характеристики;
                            ShtrihM.PrintString();
                        }

                        if (
                            RegisterCheckPosition("", Convert.ToDecimal(DataSet.СуммаБезСкидки),
                                                  Convert.ToDecimal(DataSet.СуммаСоСкидкой),
                                                  (double)(DataSet.ЦелоеКоличество + ((double) DataSet.ДробноеКоличество)/((double) DataSet.ДелимостьНаЧасти))) == false)

                            deTotalDiscount += (DataSet.СуммаБезСкидки.Value - DataSet.СуммаСоСкидкой.Value);
                    }

                    ShtrihM.StringForPrinting = "------------------------------------";
                    ShtrihM.PrintString();


                    if (deTotalDiscount > 0)
                    {
                        ShtrihM.UseReceiptRibbon = true;
                        ShtrihM.UseJournalRibbon = true;
                        //ShtrihM.StringForPrinting = "------------------------------------";
                        //ShtrihM.PrintString();
                        //ShtrihM.StringForPrinting = "       АКЦИЯ - Скидки растут!       ";
                        //ShtrihM.PrintString();
                        //ShtrihM.StringForPrinting = "------------------------------------";
                        //ShtrihM.PrintString();
                        ShtrihM.StringForPrinting = "  Вы сэкономили";
                        ShtrihM.FontType = 4;
                        ShtrihM.PrintStringWithFont();
                        ShtrihM.StringForPrinting = (int) deTotalDiscount + " руб.";
                        for (int i = (18 - ShtrihM.StringForPrinting.Length)/2; i > 0; i--)
                            ShtrihM.StringForPrinting = " " + ShtrihM.StringForPrinting;
                        ShtrihM.PrintStringWithFont();
                        ShtrihM.StringForPrinting = "------------------------------------";
                        ShtrihM.PrintString();
                    }

                    ShtrihM.Summ1 = (deTotalAmount - deCertificateAmount <= 0)
                                        ? (0)
                                        : (deTotalAmount - deCertificateAmount);
                    ShtrihM.Summ2 = 0;
                    ShtrihM.Summ3 = (deCertificateAmount > deTotalAmount) ? (deTotalAmount) : (deCertificateAmount);
                    ShtrihM.Summ4 = 0;
                    ShtrihM.DiscountOnCheck = 0;
                    ShtrihM.StringForPrinting = "";
                    if (ShtrihM.CloseCheck() != 0)
                    {
                        MessageBox.Show(ShtrihM.ResultCodeDescription, "Jet-Розница", MessageBoxButtons.OK,MessageBoxIcon.Error);
                    }
                    else
                    {
                        //DB.ЗакрытьЧек(iCheckKey, sCheckNumber, deTotalAmount, deTotalAmount, 0);//**
                        try
                        {
                            DB.ЗакрытьЧек(iCheckKey, sCheckNumber, deTotalAmount, deTotalAmount, 0);
                        }
                        catch (Exception e2)
                        {
                            try
                            {
                                DB.ЗакрытьЧек(iCheckKey, sCheckNumber, deTotalAmount, deTotalAmount); // ориганальный (старый) вариант 
                            }
                            catch (Exception)
                            {
                            }
                        } 


                        DB.ПривязатьСертификат(iCheckKey, iContractor,
                                               (deCertificateAmount > deTotalAmount)
                                                   ? (deTotalAmount)
                                                   : (deCertificateAmount));
                        DialogResult = DialogResult.OK;
                        Close();
                    }
                }
            }
            catch (Exception e2)
            {
                MessageBox.Show(e2.Message, "Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            decimal deCardSum=0;
            decimal deSertificateSum=0;

            if (iRegistratorType==3)
            {

                MessageBox.Show("Доплата карточкой не поддерживается. Необходимо внести доплату наличными", "Jet-Retail", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
                //deCardSum = (deTotalAmount - deCertificateAmount <= 0) ? (0) : (deTotalAmount - deCertificateAmount);
                //deSertificateSum = (deCertificateAmount > deTotalAmount) ? (deTotalAmount) : (deCertificateAmount);
                //MainForm.Instance.RegisterCheckPositions(0, deCardSum, true, false, null, deSertificateSum);
            }

            if (iRegistratorType == 4)
            {
               deCardSum = (deTotalAmount - deCertificateAmount <= 0) ? (0) : (deTotalAmount - deCertificateAmount);
               deSertificateSum = (deCertificateAmount > deTotalAmount) ? (deTotalAmount) : (deCertificateAmount);

                MainForm.Instance.OpenDrawer(); // открываем денежный ящик

                if (MainForm.Instance.RegisterCheckPositions(0, deCardSum, true, false, null, deSertificateSum, ""))
                {
                    string sCheckNumber = MainForm.Instance.GetRegistratorCheckNumber();

                    //if (CloseRegistratorCheck(deAccepted, 0) == true) CloseCheck(sCheckNumber, 0);

                    //DB.ЗакрытьЧек(iCheckKey, sCheckNumber, deTotalAmount, deTotalAmount, 0);
                    try
                    {
                        DB.ЗакрытьЧек(iCheckKey, sCheckNumber, deTotalAmount, 0, 0);
                    }
                    catch (Exception e2)
                    {
                        try
                        {
                            DB.ЗакрытьЧек(iCheckKey, sCheckNumber, deTotalAmount, 0); // ориганальный (старый) вариант 
                        }
                        catch (Exception)
                        {
                        }
                    } 

                    DB.ПривязатьСертификат(iCheckKey, iContractor, (deCertificateAmount > deTotalAmount) ? (deTotalAmount) : (deCertificateAmount));
                    DialogResult = DialogResult.OK;
                    Close();
                }
                return;
            }

           

            //====================================================
            decimal deTotalDiscount = 0;

            //ShtrihM.Password = 30;
            ShtrihM.Password = WorkSet.GetSchtrichPass(); 
            ShtrihM.SysAdminCancelCheck();

            //ShtrihM.Password = 1;
            ShtrihM.Password = WorkSet.GetSchtrichPassKassir(); 
            ShtrihM.CheckType = 0;
            if (ShtrihM.OpenCheck() != 0) MessageBox.Show(ShtrihM.ResultCodeDescription, "Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                //ISingleResult<ПолучитьПозицииЧекаResult> Result = DB.ПолучитьПозицииЧека(iCheckKey);
                ISingleResult<ПолучитьПозицииЧекаResult> Result = DB.GetPositionCheck(iCheckKey);
                foreach (ПолучитьПозицииЧекаResult DataSet in Result)
                {
                    string sName = DataSet.Название;
                    //string sQty = "   * " + ((DataSet.ЦелоеКоличество == 0) ? ("") : ("" + DataSet.ЦелоеКоличество)) + ((DataSet.ДробноеКоличество % DataSet.ДелимостьНаЧасти == 0) ? ("") : (((DataSet.ЦелоеКоличество == 0) ? ("") : (" и ")) + DataSet.ДробноеКоличество + "/" + DataSet.ДелимостьНаЧасти));
                    if (sName.Length > 40 - /*sQty.Length*/10) sName = sName.Substring(0, 40 - /*sQty.Length*/10);
                    //sName += sQty;

                    ShtrihM.UseReceiptRibbon = true;
                    ShtrihM.UseJournalRibbon = true;
                    ShtrihM.StringForPrinting = "------------------------------------";
                    ShtrihM.PrintString();
                    ShtrihM.StringForPrinting = DataSet.Название.Substring(0, (DataSet.Название.Length <= 36) ? (DataSet.Название.Length) : (36));
                    ShtrihM.PrintString();
                    if (DataSet.Характеристики.Length > 0)
                    {
                        ShtrihM.StringForPrinting = DataSet.Характеристики;
                        ShtrihM.PrintString();
                    }

                    if (RegisterCheckPosition("", Convert.ToDecimal(DataSet.СуммаБезСкидки), Convert.ToDecimal(DataSet.СуммаСоСкидкой), (double)(DataSet.ЦелоеКоличество + ((double)DataSet.ДробноеКоличество) / ((double)DataSet.ДелимостьНаЧасти))) == false) 

                    deTotalDiscount += (DataSet.СуммаБезСкидки.Value - DataSet.СуммаСоСкидкой.Value);
                }

                ShtrihM.StringForPrinting = "------------------------------------";
                ShtrihM.PrintString();


                if (deTotalDiscount > 0)
                {
                    ShtrihM.UseReceiptRibbon = true;
                    ShtrihM.UseJournalRibbon = true;
                    //ShtrihM.StringForPrinting = "------------------------------------";
                    //ShtrihM.PrintString();
                    //ShtrihM.StringForPrinting = "       АКЦИЯ - Скидки растут!       ";
                    //ShtrihM.PrintString();
                    //ShtrihM.StringForPrinting = "------------------------------------";
                    //ShtrihM.PrintString();
                    ShtrihM.StringForPrinting = "  Вы сэкономили";
                    ShtrihM.FontType = 4;
                    ShtrihM.PrintStringWithFont();
                    ShtrihM.StringForPrinting = (int)deTotalDiscount + " руб.";
                    for (int i = (18 - ShtrihM.StringForPrinting.Length) / 2; i > 0; i--) ShtrihM.StringForPrinting = " " + ShtrihM.StringForPrinting;
                    ShtrihM.PrintStringWithFont();
                    ShtrihM.StringForPrinting = "------------------------------------";
                    ShtrihM.PrintString();
                }

                ShtrihM.Summ1 = 0;
                ShtrihM.Summ2 = (deTotalAmount - deCertificateAmount <= 0) ? (0) : (deTotalAmount - deCertificateAmount);
                ShtrihM.Summ3 = (deCertificateAmount > deTotalAmount) ? (deTotalAmount) : (deCertificateAmount);
                ShtrihM.Summ4 = 0;
                ShtrihM.DiscountOnCheck = 0;
                ShtrihM.StringForPrinting = "";
                if (ShtrihM.CloseCheck() != 0)
                {
                    MessageBox.Show(ShtrihM.ResultCodeDescription, "Jet-Розница", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    //DB.ЗакрытьЧек(iCheckKey, sCheckNumber, deTotalAmount, 0, 0);

                    try
                    {
                        DB.ЗакрытьЧек(iCheckKey, sCheckNumber, deTotalAmount, 0, 0);
                    }
                    catch (Exception e2)
                    {
                        try
                        {
                            DB.ЗакрытьЧек(iCheckKey, sCheckNumber, deTotalAmount, 0); // ориганальный (старый) вариант 
                        }
                        catch (Exception)
                        {
                        }
                    } 

                    DB.ПривязатьСертификат(iCheckKey, iContractor, (deCertificateAmount > deTotalAmount) ? (deTotalAmount) : (deCertificateAmount));
                    DialogResult = DialogResult.OK;
                    Close();
                }
            }
        }

        private void RedeemCertificateForm_Load(object sender, EventArgs e)
        {
            textBox1.Text = DecimalToString(deTotalAmount);
            
            LoadCertificate();

            if (iRegistratorType == 3)
            {
                btnCard.Enabled = false;
            }

            if (comboBox1.SelectedIndex == -1)
            {
                btnCard.Enabled = false;
                btnCash.Enabled = false;
            }

            #region old

            //int iCompany= WorkSet.GetTypeCompany();
            //comboBox1.Items.Clear();
            //switch (iCompany)
            //{
            //    case 6:
            //        comboBox1.Items.Add("Фармин – Сертификат на [1.000.000]");
            //        comboBox1.Items.Add("Фармин – Сертификат на [700.000]");
            //        break;
            //    case 4:
            //        comboBox1.Items.Add("Ликва – Сертификат на [200.000]");
            //        break;
            //    default:
            //        comboBox1.Items.Add("Фармин – Сертификат на [1.000.000]");
            //        comboBox1.Items.Add("Фармин – Сертификат на [700.000]");
            //        comboBox1.Items.Add("Ликва – Сертификат на [200.000]");
            //        break;
            //}

            #endregion           
        }

        private void LoadCertificate()
        {
            comboBox1.Items.Clear();

            int iCompany = WorkSet.GetTypeCompany();

            ISingleResult<CertificatListResult> Result = DB.GetCertificatList();
            foreach (CertificatListResult DataSet in Result)
            {
                //if (iCompany == DataSet.Certificates_TypeCompany)
                //{
                    comboBox1.Items.Add(DataSet.Certificates_Name + "  " + DataSet.Certificates_Nominal);
                    lstSumCertificate.Add(Convert.ToDecimal(DataSet.Certificates_Nominal));
                    lstContractor.Add(Convert.ToInt32(DataSet.Certificates_TypeCompany));
                //}
            }

        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (((iContractor = comboBox1.SelectedIndex + 1) == 2) | ((iContractor = comboBox1.SelectedIndex + 1) == 5)) deCertificateAmount = 50000;
            //else deCertificateAmount = 100000;
            //textBox1.Text = DecimalToString((deTotalAmount - deCertificateAmount <= 0) ? (0) : (deTotalAmount - deCertificateAmount));

            if (comboBox1.Items.Count != 0)
            {
                deCertificateAmount = lstSumCertificate[comboBox1.SelectedIndex];
                iContractor = lstContractor[comboBox1.SelectedIndex];

                textBox1.Text = DecimalToString((deTotalAmount - deCertificateAmount <= 0) ? (0) : (deTotalAmount - deCertificateAmount));

               

            }

            if (comboBox1.SelectedIndex != -1)
            {
                if (iRegistratorType == 3)
                {
                    btnCash.Enabled = true;
                }

                if (iRegistratorType == 2)
                {
                    btnCash.Enabled = true;
                    btnCard.Enabled = true;
                }

                if (iRegistratorType==4)
                {
                    btnCash.Enabled = true;
                    btnCard.Enabled = true;
                }
            }

            #region old

            //int iCompany = WorkSet.GetTypeCompany();

            //switch (iCompany)
            //{
            //    case 6:
            //        if (comboBox1.SelectedIndex + 1 == 1)
            //        {
            //            iContractor = 6;
            //            deCertificateAmount = 1000000;
            //        }
            //        if ((comboBox1.SelectedIndex + 1) == 2)
            //        {
            //            iContractor = 6;
            //            deCertificateAmount = 700000;
            //        }
            //        textBox1.Text = DecimalToString((deTotalAmount - deCertificateAmount <= 0) ? (0) : (deTotalAmount - deCertificateAmount));
            //        break;

            //    case 4:
            //        if ((comboBox1.SelectedIndex + 1) == 1)
            //        {
            //            iContractor = 4;
            //            deCertificateAmount = 200000;
            //        }
            //        textBox1.Text = DecimalToString((deTotalAmount - deCertificateAmount <= 0) ? (0) : (deTotalAmount - deCertificateAmount));
            //        break;
            //   default:
            //         if (comboBox1.SelectedIndex + 1 == 1)
            //         {
            //            iContractor = 6;
            //            deCertificateAmount = 1000000;
            //         }
            //         if ((comboBox1.SelectedIndex + 1) == 2)
            //         {
            //            iContractor = 6;
            //            deCertificateAmount = 700000;
            //         }
            //         if ((comboBox1.SelectedIndex + 1) == 3)
            //         {
            //            iContractor = 4;
            //            deCertificateAmount = 200000;
            //         }
            //         textBox1.Text = DecimalToString((deTotalAmount - deCertificateAmount <= 0) ? (0) : (deTotalAmount - deCertificateAmount));
            //         break;   
            //}

            #endregion

            
        }


    }
}